/*********************************************************************************
 *      Copyright:  (C) 2022 YanlinZhou<1222>
 *                  All rights reserved.
 *
 *       Filename:  sqlite.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/09/2022)
 *         Author:  YanlinZhou <1222>
 *      ChangeLog:  1, Release initial version on "04/09/2022 05:32:33 PM"
 *                 
 ********************************************************************************/

#include "sqlite.h"


/***************************Funtion declaration******************************
| * Function nam: create_database_and_table
| * Funtion performance: Creat a database
| * Parameter: NULL
| * Returned value:A database fd
| * Description: Creat a database named temp.db and create a table name temp
*****************************************************************************/
int create_database_and_table(sqlite3 **db)
{
	char 			*sql;
	int				rv = -1;

     rv = sqlite3_open("temperature.db",db);
     if( rv )
     {
		 printf("Opend  database failure:%s\n", strerror(errno));
         sqlite3_close(*db);
		 return -1;
     }
     else
	 { 
		 printf("You have opened a sqlite3 database named temperature.db successfully!\n");
		 return 0;
	 }

	 sql = "CREATE TABLE IF NOT EXISTS temp_tb(Piname TEXT, temp TEXT,Time TEXT);" ;
     rv = sqlite3_exec(*db,sql,NULL,NULL,NULL);
	 if (rv) 
	 {
		 printf ("Creat table failure:%s\n", strerror(errno));
		 return -2;
	 }
	 else
	 {
		 printf("create temp_tb table sucessfully!\n");
		 return 0;
	 }

}


/***************************Funtion declaration******************************
| * Function nam: server_save_data
| * Funtion performance: Save the server received data to the databasen
| * Parameter: char *sample_data
| * Returned value:
| * Description: NULL
*****************************************************************************/
int  server_save_data(char *name, char *temp, char *time, sqlite3 *db)
{
	char	str[128] = {0};
	int rv =- 1;
    sprintf(str, "insert into temp_tb values('%s','%s','%s')", name, temp, time);
    rv = sqlite3_exec(db, str, 0, 0, NULL);

//	printf ("receive str = : %s\n", str);

    if(rv < 0)
    {
        printf("insert data to database temp_tb failure!\n");
        return -1;
    }
//    printf ("Insert data into database temp_tb sucessfully!\n");
    return 0;
}


/***************************Funtion declaration******************************
| * Function nam: create a database
| * Funtion performance: Creat a database
| * Parameter: NULL
| * Returned value:A database fd
| * Description: Creat a database named temp.db and create a table name temp
*****************************************************************************/
int create_database(sqlite3 **db)
{
    char            *sql;
    int             rv = -1;

     rv = sqlite3_open("temperature.db",db);
     if( rv )
     {
         printf("Opend  database failure:%s\n", strerror(errno));
         sqlite3_close(*db);
         return -1;
     }
     else
     {
         printf("You have opened database named temperature.db successfully!\n");
         return 0;
     }
}


/***************************Funtion declaration******************************
| * Function nam: create a table 
| * Funtion performance: Creat a table
| * Parameter: NULL
| * Returned value:A database fd
| * Description: Creat a database named temperature.db and create a table name temp_tb
*****************************************************************************/
int create_table(sqlite3 *db)
{
    char *sql;
    int rv = -1;
     sql ="CREATE TABLE IF NOT EXISTS temp_tb(Piname TEXT, Temperature TEXT, Time TEXT);" ;
     rv = sqlite3_exec(db,sql,NULL,NULL,NULL);
     if (rv)
     {
         printf ("Creat table failure:%s\n", strerror(errno));
         return -1;
     }
     else
     {
         printf("create temp_tb table sucessfully!\n");
         return 0;
     }
}

